﻿
using Newtonsoft.Json;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace HenrryTools.WinFormTools
{
    public partial class FormatWinForm : Form
    {
        public FormatWinForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 运行序列化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFormat_Click(object sender, EventArgs e)
        {
            SendFromat();

        }
        private void SendFromat()
        {
            if (string.IsNullOrWhiteSpace(txtRoot.Text))
            {
                labMessage.Text = "你的输入为空,请输入";
                labMessage.ForeColor = Color.Red;
                return;
            }
            try {
                txtRoot.Text = JsonConvert.SerializeObject(txtRoot.Text);
                labMessage.Text = "序列化成功,剪切板已复制!";
                labMessage.ForeColor = Color.Green;
                Clipboard.SetDataObject(txtRoot.Text);//复制
                Properties.Settings.Default.FormatWinFormTxt = txtRoot.Text;//保存在Settings
                Properties.Settings.Default.Save();//保存
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 运行反序列化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUnFormat_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtRoot.Text))
            {
                labMessage.Text = "你的输入为空,请输入";
                labMessage.ForeColor = Color.Red;
                return;
            }
            try
            {
                txtRoot.Text = JsonConvert.DeserializeObject(txtRoot.Text).ToString();
                labMessage.Text = "反序列化成功!";
                labMessage.ForeColor = Color.Green;

                Properties.Settings.Default.FormatWinFormTxt = txtRoot.Text;//保存在Settings
                Properties.Settings.Default.Save();//保存
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 按钮监控
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtRoot_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                SendFromat();
            }
        }
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormatWinForm_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.FormatWinFormTxt))
            {
                txtRoot.Text = Properties.Settings.Default.FormatWinFormTxt;
            }
        }
    }
}
